
package xj.graph2d.app.prop;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class GridPanel 
  extends JPanel { 

  public GridPanel() {
    this(true);
  }

  public GridPanel(boolean opaque) {
    setOpaque(opaque);
    setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    //setBackground(Color.white);
    setLayout(gbl = new GridBagLayout());
    row = 0;
  }

  void addRow(String left, JComponent right) {
    addRow(left, right, false);
  }

  void addRow(JComponent left, JComponent right) {
    addRow(left, right, false);
  }

  void addRow(String left, JComponent right, boolean center) {
    addRow(left, right, center ? GridBagConstraints.CENTER : GridBagConstraints.WEST);
  }

  void addRow(JComponent left, JComponent right, boolean center) {
    addRow(left, right, center ? GridBagConstraints.CENTER : GridBagConstraints.WEST);
  }

  void addRow(String left, JComponent right, int pos) {
    JLabel label = null;

    if (left != null) {
      label = new JLabel(left);
      //label.setFont(Style.font);
      //label.setBackground(Color.white);
      if (showBorder) {
	label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
      } else {
	label.setBorder(null);
      }
      label.setVerticalAlignment(SwingConstants.CENTER);
    }
    addRow(label, right, pos);
  }

  void addRow(JComponent left, JComponent right, int pos) {
    if (right != null) {

      if (showBorder) {
	right.setBorder(BorderFactory.createLineBorder(Color.lightGray));
      }

      GridBagConstraints c = new GridBagConstraints();
      // c.anchor = GridBagConstraints.NORTH;
      // c.anchor = GridBagConstraints.WEST;
      c.anchor = pos;
      c.fill = GridBagConstraints.NONE;
      // c.gridy = propEntries.size();
      c.gridy = row++;
      c.gridx = 0;
      c.ipadx = 2;
      c.weightx = 0;
      c.weighty = 0;
      c.insets = new Insets(2, 2, 2, 2);
      if (left != null) {
	add(left);
	gbl.setConstraints(left, c);
	c.gridx = 1;
      }

      if (!isEditButton(right)) { 
	c.fill = GridBagConstraints.HORIZONTAL;
      }
      c.weightx = 1;
      c.weighty = 0;
      add(right);
      gbl.setConstraints(right, c);

      c.gridx++; 
      c.fill = GridBagConstraints.HORIZONTAL;
      Component filler = Box.createHorizontalGlue();
      add(filler);
      gbl.setConstraints(filler, c);
    }
  }

  void addRow(JComponent comp) {
    addRow(comp, false);
  }

  void addRow(JComponent comp, boolean center) {
    addRow(comp, center ? GridBagConstraints.CENTER : GridBagConstraints.WEST);
  }

  void addRow(JComponent comp, int pos) {
    if (comp != null) {
      if (showBorder) {
	comp.setBorder(BorderFactory.createLineBorder(Color.lightGray));
      }

      GridBagConstraints c = new GridBagConstraints();
      c.anchor = pos;
      if (!isEditButton(comp)) { 
	c.fill = GridBagConstraints.HORIZONTAL;
      }
      c.gridy = row++;
      c.gridx = 0;
      c.ipadx = 2;
      c.gridwidth = 2;
      c.weighty = 1;
      add(comp);
      gbl.setConstraints(comp, c);

      c.gridx++; 
      c.fill = GridBagConstraints.HORIZONTAL;
      Component filler = Box.createHorizontalGlue();
      add(filler);
      gbl.setConstraints(filler, c);
    }
  }

  void addRow(ModelPropertyField field) {
    if (field != null) {
      JComponent comp = field.getEditor();
      if (comp != null) { 
	comp.setName(field.getFieldName());
      }
      addRow(field.getDisplayName(), comp, GridBagConstraints.WEST);
    }
  }

  void addFiller() {
    JLabel filler = new JLabel();
    GridBagConstraints c = new GridBagConstraints();
    // c.gridy = propEntries.size();
    c.gridy = row++;
    c.gridx = 0;
    c.gridwidth = 3;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    add(filler);
    gbl.setConstraints(filler, c);
  }

  boolean isEditButton(JComponent comp) { 
    if (comp instanceof JButton) { 
      JButton b= (JButton) comp;
      if ("Edit".equals(b.getText())) { 
	return true;
      }
    }
    return false;
  }

  protected GridBagLayout gbl;

  protected boolean showBorder = false;

  protected int row = 0;

}

